package com.github.boluo1230.wechat.service.mp.impl;

import com.github.boluo1230.wechat.dao.QrAutoReplyInfoDao;
import com.github.boluo1230.wechat.entity.EventMessage;
import com.github.boluo1230.wechat.entity.MpApp;
import com.github.boluo1230.wechat.entity.QrAutoReplyInfo;
import com.github.boluo1230.wechat.entity.detail.ReplyDetails;
import com.github.boluo1230.wechat.service.mp.QrAutoReplyService;
import com.github.boluo1230.wechat.service.mp.RedisService;
import com.github.boluo1230.wechat.service.mp.SendMessageServive;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Code Farmer
 * @date 2019/10/16 10:19
 */
@Service
public class QrAutoReplyServiceImpl implements QrAutoReplyService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private QrAutoReplyInfoDao qrAutoReplyInfoDao;
    @Autowired
    private SendMessageServive sendMessageServive;

    @Override
    public boolean handleQrAutoReply(EventMessage message, MpApp mpApp) {

        Map<String, Boolean> checkMap = new ConcurrentHashMap<>();
        checkMap.put("subscribe", true);
        checkMap.put("scan", true);
        boolean eventBoolean = false;
        if (StringUtils.isNotBlank(message.getEvent()) && checkMap.get(message.getEvent().toLowerCase())) {
            eventBoolean = true;
        }

        if (!message.getMsgType().equals("event") || StringUtils.isBlank(message.getEventKey()) || !eventBoolean) {
            return false;
        }

        String eventKey = message.getEventKey().replace("qrscene_", "");
        String appId = mpApp.getAppId();
        QrAutoReplyInfo qrAutoReplyInfo = this.findQrAutoReplyConfig(appId, eventKey);

        //如果没有配置自动回复，或者是取消订阅消息，则直接跳过
        if (qrAutoReplyInfo == null) {
            return false;
        }

        //没有配置回复内容直接退出
        List<ReplyDetails> replyDetailsList = qrAutoReplyInfo.getReplyListInfo();
        if (replyDetailsList == null || replyDetailsList.size() == 0) {
            return false;
        }

        List<ReplyDetails> newReplyList = new ArrayList<>();
        if (qrAutoReplyInfo.getReplyMode().equals("random_one")) {
            int random = (int) Math.ceil(replyDetailsList.size() - 1);
            newReplyList.add(replyDetailsList.get(random));
        } else if (qrAutoReplyInfo.getReplyMode().equals("reply_all")) {
            newReplyList.addAll(replyDetailsList);
        }
        sendMessageServive.sendReplyList(mpApp, message.getFromUserName(), newReplyList);
        return newReplyList.size() > 0;
    }

    private QrAutoReplyInfo findQrAutoReplyConfig(String appId, String eventKey) {
        QrAutoReplyInfo qrAutoReplyInfo = redisService.getCachedMpAppQrAutoReplyConfig(appId, eventKey);
        if (qrAutoReplyInfo == null) {
            qrAutoReplyInfo = qrAutoReplyInfoDao.getQrByAppIdAndEventKey(appId, eventKey);
            if (qrAutoReplyInfo == null) {
                return null;
            }
            redisService.setCachedMpAppQrAutoReplyConfig(appId, eventKey, qrAutoReplyInfo);
        }
        return qrAutoReplyInfo;
    }
}
